<?php

namespace Yolo\Test;

use Hyperf\Testing\Http\TestResponse;
use Hyperf\Testing\TestCase;
use function Hyperf\Support\env;

/**
 * AbstractTest
 * @author lushaoming<lushaoming6@gmail.com>
 * @date 2024/1/26
 */
abstract class AbstractTest extends TestCase
{
    /**
     * @var string 服务名称，如果没设置，则使用环境变量SERVICE_NAME
     */
    protected string $service;

    protected RPCClient $rpcClient;

    public function __construct(string $name)
    {
        parent::__construct($name);
        if (empty($this->service)) {
            $this->service = env('SERVICE_NAME', '');
        }

        $this->rpcClient = new RPCClient($this->service);
    }

    public function invokeByNs(string $ns, string $api, array $args, array $metadata): TestResponse
    {
        $response = $this->rpcClient->invokeByNs($ns, $api, $args, $metadata);

        return $this->createTestResponse($response);
    }

    protected function createTestResponse($response): TestResponse
    {
        return new TestResponse($response);
    }
}